home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snpd9611.zip / SEQTOUCH.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  6KB  |  203 lines

  1. /* +++Date last modified: 17-Nov-1996 */
  2.  
  3. /*
  4. **  SEQTOUCH.C - Touch files in a directory with sequential time stamps.
  5. **
  6. **  Public domain by Bob Stout
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <conio.h>
  12. #include <string.h>
  13. #ifdef __TURBOC__
  14.  #include <dir.h>
  15. #else
  16.  #include <direct.h>
  17. #endif
  18. #include "unistd.h"
  19. #include "ftime.h"
  20. #include "datetime.h"
  21. #include "getopts.h"
  22. #include "errors.h"
  23. #include "snipfile.h"
  24. #include "dosfiles.h"
  25. #include "snipkbio.h"
  26. #include "dirport.h"
  27.  
  28. /*
  29. **  Options area - uses getopts() from SNIPPETS.
  30. */
  31.  
  32. char fname[FILENAME_MAX] = "";
  33. char pname[FILENAME_MAX] = "";
  34. char datestr[40];
  35. char timestr[20];
  36.  
  37. Boolean_T help    = False_;
  38. Boolean_T noquery = False_;
  39.  
  40. struct Option_Tag options[] = {
  41.       {'F',  False_, String_Tag,  fname,    NULL, NULL, NULL},
  42.       {'P',  False_, String_Tag,  pname,    NULL, NULL, NULL},
  43.       {'D',  False_, String_Tag,  datestr,  NULL, NULL, NULL},
  44.       {'T',  False_, String_Tag,  timestr,  NULL, NULL, NULL},
  45.       {'H',  False_, Boolean_Tag, &help,    NULL, NULL, NULL},
  46.       {'N',  False_, Boolean_Tag, &noquery, NULL, NULL, NULL},
  47.       {'\0', False_, Error_Tag,   NULL,     NULL, NULL, NULL}
  48. };
  49.  
  50. void loadtm(struct tm *ftm, struct ftime *ftimep)
  51. {
  52.       ftimep->ft_tsec  = ftm->tm_sec / 2;
  53.       ftimep->ft_min   = ftm->tm_min;
  54.       ftimep->ft_hour  = ftm->tm_hour;
  55.       ftimep->ft_day   = ftm->tm_mday;
  56.       ftimep->ft_month = ftm->tm_mon + 1;
  57.       ftimep->ft_year  = ftm->tm_year - 80;
  58. }
  59.  
  60. void usage(int errlvl)
  61. {
  62.       puts("SEQTOUCH - A utility to process a directory so its files "
  63.            "have sequentially");
  64.       puts("           increasing time/date stamps\n");
  65.       puts("Usage: SEQTOUCH [-N] [-Ddir] [-Ffile] [-Dmm-dd-yy] "
  66.            "[-Thh:mm:ss]\n");
  67.       puts("Where: <dir> is the directory to process");
  68.       puts("       <file> is an existing file whose time/date stamp "
  69.            "to use");
  70.       puts("       <mm-dd-yy> is a date in month-day-year format");
  71.       puts("       <hh:mm:ss> is a time in hours:minutes:seconds "
  72.            "format\n");
  73.       puts("       <-N> suppresses verification of arguments\n");
  74.       puts("All are optional. Specifying SEQTOUCH with no arguments "
  75.            "causes all files in");
  76.       puts("the current directory to be dated to the current date. "
  77.            "The timestamp of the");
  78.       puts("first file will be set to the current date and time and "
  79.            "each subsequent file");
  80.       puts("will have its time/date stamp set to 2 seconds later "
  81.            "than the preceding file.\n");
  82.       puts("Specifying the -T and/or -D options sets the time and/or date "
  83.            "of the first");
  84.       puts("file in the directory to the speified time and/or date.\n");
  85.       puts("Specifying a file with the -F option causes the timestamp of "
  86.            "first file in ");
  87.       puts("the directory to be set to match the specified file.\n");
  88.       puts("If the -N option is not specified, you will br prompted with "
  89.            "the specific");
  90.       puts("directory, time, and date (and file, if specified) to be used.");
  91.       puts("The option switches are not case sensitive, i.e. -f works just "
  92.            "like -F.");
  93.       exit(errlvl);
  94. }
  95.  
  96. int main(int argc, char *argv[])
  97. {
  98.       struct ftime ftimep;
  99.       struct tm ftm;
  100.       time_t ft;
  101.       DOSFileData ff;
  102.       char str[50];
  103.       char path[FILENAME_MAX];
  104.       char file[FILENAME_MAX];
  105.       size_t plen;
  106.  
  107.       if (Error_ == getopts(argc, argv))
  108.             usage(EXIT_FAILURE);
  109.  
  110.       if (help)
  111.             usage(EXIT_SUCCESS);
  112.  
  113.       if (NUL != pname[0])
  114.       {
  115.             if (!isdir(pname))
  116.                   ErrExit("%s is not a directory", pname);
  117.       }
  118.       else  getcwd(pname, FILENAME_MAX);
  119.  
  120.       if (NUL != fname[0])
  121.       {
  122.             FILE *fp;
  123.  
  124.             fp = cant(fname, "r");
  125.             if (Success_ != getftime(fileno(fp), &ftimep))
  126.                   ErrExit("Can't read timestamp of %s", fname);
  127.             ftime2tm(&ftimep, &ftm);
  128.             ft = ftime2time(&ftimep);
  129.       }
  130.       else
  131.       {
  132.             time(&ft);
  133.             ftm = *localtime(&ft);
  134.  
  135.             if (NUL != datestr[0])
  136.             {
  137.                   unsigned yy, mm, dd;
  138.  
  139.                   if (Success_ != parse_date(datestr, &yy, &mm, &dd, USA))
  140.                         ErrExit("Invalid date - %s", datestr);
  141.                   ftm.tm_year = yy - 1900;
  142.                   ftm.tm_mon  = mm - 1;
  143.                   ftm.tm_mday = dd;
  144.             }
  145.  
  146.             if (NUL != timestr[0])
  147.             {
  148.                   unsigned  hh, mm, ss;
  149.  
  150.                   if (Success_ != parse_time(timestr, &hh, &mm, &ss))
  151.                         ErrExit("Invalid time - %s", timestr);
  152.                   ftm.tm_hour = hh;
  153.                   ftm.tm_min  = mm;
  154.                   ftm.tm_sec  = ss;
  155.             }
  156.  
  157.             loadtm(&ftm, &ftimep);
  158.       }
  159.  
  160.       printf("\nDirectory %s\n", pname);
  161.       if (NUL == fname[0])
  162.             puts("No file specified\n");
  163.       else  printf("Using file %s\n\n", fname);
  164.       strftime(str, 50, "%A, %d-%b-%Y, %X", &ftm);
  165.       printf("Files will be timstamped sequentially beginning with\n  %s\n",
  166.              str);
  167.       if (!noquery)
  168.       {
  169.             if (False_ == getYN("OK?", 'N', 5))
  170.             {
  171.                   fputs("\nSEQTOUCH aborted by user\n", stderr);
  172.                   return EXIT_SUCCESS;
  173.             }
  174.       }
  175.  
  176.       strcpy(path, pname);
  177.       if ('\\' != LAST_CHAR(path))
  178.             strcat(path, "\\");
  179.       plen = strlen(path);
  180.       strcpy(file, path);
  181.  
  182.       strcat(path, "*.*");
  183.       if (Success_ == FIND_FIRST(path, _A_NORMAL, &ff)) do
  184.       {
  185.             FILE *fp;
  186.  
  187.             file[plen] = NUL;
  188.             strcat(file, ff_name(&ff));
  189.  
  190.             fprintf(stderr, "Touching %s\n", file);
  191.  
  192.             fp = cant(file, "r+");
  193.             setftime(fileno(fp), &ftimep);
  194.             fclose(fp);
  195.  
  196.             ftm.tm_sec += 2;
  197.             ft = mktime(&ftm);            /* Used to normalize time     */
  198.             loadtm(&ftm, &ftimep);
  199.       } while (Success_ == FIND_NEXT(&ff));
  200.  
  201.       return EXIT_SUCCESS;
  202. }
  203.